package com.example.controller;

import com.example.entity.*;
import com.example.service.SchemeService;
import com.example.util.JsonBuilder;
import com.example.util.JsonPaser;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Iterator;
import java.util.List;
import java.util.Map;


@RestController
public class SchemeController {

    @Autowired
    private SchemeService schemeService;

    JsonBuilder jsonBuilder = new JsonBuilder();
    JsonPaser jsonPaser = new JsonPaser();

    @ResponseBody
    @RequestMapping(value = "/GetArmyByID", method = {RequestMethod.POST})
    public String GetArmyByID(@Param("army_id") Integer army_id) {
        return jsonBuilder.buildArmy(schemeService.RequestArmyByID(army_id));
    }

    @ResponseBody
    @RequestMapping(value = "/GetArmy", method = {RequestMethod.POST})
    public String GetArmy(@Param("user_id") Integer user_id) {
        return jsonBuilder.buildArmyList(schemeService.RequestArmy());
    }

    @ResponseBody
    @RequestMapping(value = "/GetLocation", method = {RequestMethod.POST})
    public String GetLocation(
            @Param("user_id") Integer user_id,
            @Param("province") String province,
            @Param("city") String city,
            @Param("county") String county) {
        System.out.println(province + "..." + city + "..." + county);
        Location location = new Location(0, province, city, county);

        return jsonBuilder.buildLocationList(schemeService.RequestLocation(location));
    }

    @ResponseBody
    @RequestMapping(value = "/CreateScheme", method = {RequestMethod.POST})
    public String CreateScheme(@Param("user_id") Integer user_id) {
        Scheme scheme = new Scheme();
        scheme.setScheme_id(schemeService.CreateScheme());
        return jsonBuilder.buildScheme(scheme);
    }

    @ResponseBody
    @RequestMapping(value = "/AddSchemeMsg", method = {RequestMethod.POST}, produces = "application/json;charset=UTF-8")
    public String AddSchemeMsg(@RequestBody String schemeJson) {
        Scheme scheme = jsonPaser.ParseScheme(schemeJson);

        if (schemeService.AddSchemeMsg(scheme)) {
            Integer scheme_id = scheme.getScheme_id();
            scheme = new Scheme();
            scheme.setScheme_id(scheme_id);
            return jsonBuilder.buildScheme(scheme);
        } else {
            return "{'type_code':'error'}";
        }
    }

    @ResponseBody
    @RequestMapping(value = "/AddTask", method = {RequestMethod.POST}, produces = "application/json;charset=UTF-8")
    public String AddTask(@RequestBody String taskJson) {
        Task task = jsonPaser.ParseTask(taskJson);
        return schemeService.AddTask(task);
    }

    @ResponseBody
    @RequestMapping(value = "/GetEnvironment", method = {RequestMethod.POST})
    public String GetEnvironment(@Param("user_id") Integer user_id, @Param("location_id") Integer location_id, @Param("month_time") Integer month_time) {
        return jsonBuilder.buildEnvironment(schemeService.RequestEnvironment(location_id, month_time), schemeService.RequestLocationByID(location_id));
    }

    @ResponseBody
    @RequestMapping(value = "/GetEquipmentMsg", method = {RequestMethod.POST})
    public String GetEnvironment(@Param("user_id") Integer user_id, @Param("army_id") Integer army_id) {
        return jsonBuilder.buildCategory(schemeService.RequestEquipment(army_id));
    }

    @ResponseBody
    @RequestMapping(value = "/SelectCarryEquipment", method = {RequestMethod.POST}, produces = "application/json;charset=UTF-8")
    public String SelectCarryEquipment(@RequestBody String idJson) {
        Map<Integer, List<Integer>> map = jsonPaser.ParseEquipmentIDList(idJson);
        Iterator iter = map.entrySet().iterator();
        if (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next();
            Integer key = (Integer) entry.getKey();
            List<Integer> val = (List<Integer>) entry.getValue();
            schemeService.AddSchemeEquipment(key, val);
            return "{'type_code':'scheme_equipment'}";
        } else {
            return "{'type_code':'error'}";
        }
    }

    @ResponseBody
    @RequestMapping(value = "/CreatPlan", method = {RequestMethod.POST}, produces = "application/json;charset=UTF-8")
    public String AddPlan(@RequestBody String planJson) {
        Plan plan = jsonPaser.ParsePlan(planJson);
        return schemeService.AddPlan(plan);
    }

    @ResponseBody
    @RequestMapping(value = "/CreatGroup", method = {RequestMethod.POST}, produces = "application/json;charset=UTF-8")
    public String AddGroup(@RequestBody String groupJson) {
        Group group = jsonPaser.ParseGroup(groupJson);
        return schemeService.AddGroup(group);
    }

    @ResponseBody
    @RequestMapping(value = "/CreatTeam", method = {RequestMethod.POST}, produces = "application/json;charset=UTF-8")
    public String AddTeam(@RequestBody String teamJson) {
        Team team = jsonPaser.ParseTeam(teamJson);
        return schemeService.AddTeam(team);
    }

    @ResponseBody
    @RequestMapping(value = "/GetDepartment", method = {RequestMethod.POST})
    public String GetDepartment(@Param("user_id") Integer user_id) {
        return jsonBuilder.buildDepartment(schemeService.RequestDepartment());
    }

    @ResponseBody
    @RequestMapping(value = "/AddTeamPerson", method = {RequestMethod.POST}, produces = "application/json;charset=UTF-8")
    public String AddTeamPerson(@RequestBody String teamDepartmentJson) {
        Team_Department list = jsonPaser.ParseTeamDepartment(teamDepartmentJson);
        return schemeService.AddTeamDepartment(list);
    }

    @ResponseBody
    @RequestMapping(value = "/AddTeamCategory", method = {RequestMethod.POST}, produces = "application/json;charset=UTF-8")
    public String AddTeamCategory(@RequestBody String teamCategoryJson) {
        Team_Category list = jsonPaser.ParseTeamCategory(teamCategoryJson);
        return schemeService.AddTeamCategory(list);
    }

    @ResponseBody
    @RequestMapping(value = "/ModifySchemeMsg", method = {RequestMethod.POST}, produces = "application/json;charset=UTF-8")
    public String ModifySchemeMsg(@RequestBody String schemeJson) {
        Scheme scheme = jsonPaser.ParseScheme(schemeJson);
        return schemeService.ModifySchemeMsg(scheme);
    }

    @ResponseBody
    @RequestMapping(value = "/ModifyTask", method = {RequestMethod.POST}, produces = "application/json;charset=UTF-8")
    public String ModifyTask(@RequestBody String json) {
        Task task = jsonPaser.ParseTask(json);
        return schemeService.ModifyTask(task);
    }

    @ResponseBody
    @RequestMapping(value = "/GetTask", method = {RequestMethod.POST})
    public String GetTask(@Param("user_id") Integer user_id, @Param("type_id") Integer type_id, @Param("type") Integer type) {
        List<Task> list = schemeService.RequestTask(type_id, type);
        return jsonBuilder.buildTask(list);
    }

    @ResponseBody
    @RequestMapping(value = "/GetPlan", method = {RequestMethod.POST})
    public String GetPlan(@Param("user_id") Integer user_id, @Param("scheme_id") Integer scheme_id) {
        List<Plan> plans = schemeService.RequestPlan(scheme_id);
        return jsonBuilder.buildPlanList(plans);
    }

    @ResponseBody
    @RequestMapping(value = "/GetGroup", method = {RequestMethod.POST})
    public String GetGroup(@Param("user_id") Integer user_id, @Param("plan_id") Integer plan_id) {
        List<Group> groups = schemeService.RequestGroup(plan_id);
        return jsonBuilder.buildGroupList(groups);
    }

    @ResponseBody
    @RequestMapping(value = "/GetTeam", method = {RequestMethod.POST})
    public String GetTeam(@Param("user_id") Integer user_id, @Param("team_id") Integer team_id) {
        List<Team> teams = schemeService.RequestTeam(team_id);
        return jsonBuilder.buildTeamList(teams);
    }

}